import { observable, action } from 'mobx';
import { Tools, Modal, Bubble } from 'weaver-mobile';
import { Form } from 'weaver-mobile-page';

import * as Api from '../../api/BlogAttentionApi'; 
const alert = Modal.alert;
const getLabel = Tools.getLabel;

export default class AttentionGroup {
  @observable groupLoading = true;
  @observable groupList = []; 
  @observable editLoading = true;
  @observable editForm = new Form.Store({ route: '/userCenter/blogAttention/attention/groupManagement', id:'new' });

  @action('分组信息')
  getMyAttentionGroup = () => {
    this.groupLoading = true;
    Api.getMyAttentionGroup().then(data => {
      this.groupList = data.groupList;
      this.groupLoading = false;
    })
  }

  @action('删除操作')
  deleteBlogGroup = (v, callback) => {
    alert(getLabel(384343,"确定要删除分组吗？"),getLabel(384458,"删除后，此分组下的人员将移至“未分组”列表。"),[
      {text: getLabel(31129, '取消')},
      {text: getLabel(33703, '确认'), onPress: () => {
        Api.deleteBlogGroup({groupId: v}).then(res => {
          if(res.status === '1') {
            this.getMyAttentionGroup();
            if (callback) callback();
          }
        })
      }}
    ])
  }

  /* 新建分组 */
  @action('新建Condition请求')
  getBlogGroupAdd = (params, pathname) => {
    this.editLoading = true;
    Api.getBlogGroupAdd(params).then(data => {
      this.editForm.initForm(data.conditioninfo, pathname)
      this.editLoading = false;
    })
  }

  @action('新建提交')
  createBlogGroup = (callback) => {
    let formParams = this.editForm.formParams;
    let validResult = this.editForm.doValidate();
    if (validResult.pass) {
      Api.createBlogGroup(formParams).then((res) => {
        if (res.groupInfo !== '') {
          this.getMyAttentionGroup();
          this.editForm.resetFields();
          Bubble.success(getLabel(30700, '操作成功')+'!', 1);
          if (callback) callback();
        } else {
          Bubble.warning(getLabel('501106','分组名称已存在！') , 1)
        }
      })
    }else {
      Bubble.error(validResult.message);
    }
  }

  @action('编辑提交')
  editBlogGroup = (params, callback) => {
    let formParams = this.editForm.formParams;
    let validResult = this.editForm.doValidate();
    params = {
      ...params,
      ...formParams,
    }
    if (validResult.pass) {
      Api.editBlogGroup(params).then((res) => {
        if (res.groupInfo) {
          this.getMyAttentionGroup();
          this.editForm.resetFields();
          Bubble.success(getLabel(30700, '操作成功')+'!', 1);
        }
        if (callback) callback();
      })
    }else {
      Bubble.error(validResult.message);
    }
  }
}